package org.myspringframework.beans.factory.support;

import org.myspringframework.beans.BeansException;
import org.myspringframework.beans.factory.BeanFactory;
import org.myspringframework.beans.factory.ConfigurableListableBeanFactory;
import org.myspringframework.beans.factory.config.beandefinition.BeanDefinition;
import org.myspringframework.beans.factory.config.beandefinition.BeanDefinitionRegistry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//保存beanDefinition注册表，实现beanDefinition的注册,实现了单例bean的预初始化
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory{

    //beanDefinition注册表
    private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();

    //注册beanDefinition到注册表
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    //从注册表获取beanDefinition
    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
        return beanDefinition;
    }

    //按照类型按照类型返回 Bean 实例
    // 在BeanDefinition注册表中查找对应的<beanName:object>集合
    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        Map<String, T> result = new HashMap<>();
        //在BeanDefinition注册表中查找对应class的beandefinition
        beanDefinitionMap.forEach((beanName, beanDefinition) -> {
            Class beanClass = beanDefinition.getBeanClass();
            //判断当前的Class对象所表示的类，是不是参数中传递的Class对象所表示的类的父类，接口，或者是相同的类型。
            if (type.isAssignableFrom(beanClass)) {
                //符合要求,获取对应的bean
                result.put(beanName, (T) getBean(beanName));
            }
        });
        return result;
    }

    //根据类型获取bean
    @Override
    public <T> T getBean(Class<T> requiredType) throws BeansException {
        List<String> beanNames = new ArrayList<>();
        for (Map.Entry<String, BeanDefinition> entry : beanDefinitionMap.entrySet()) {
            Class beanClass = entry.getValue().getBeanClass();
            if (requiredType.isAssignableFrom(beanClass)) {
                beanNames.add(entry.getKey());
            }
        }
        if (1 == beanNames.size()) {
            return getBean(beanNames.get(0), requiredType);
        }

        throw new BeansException(requiredType + "expected single bean but found " + beanNames.size() + ": " + beanNames);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return beanDefinitionMap.keySet().toArray(new String[0]);
    }

    //对单例bean进行预实例化
    @Override
    public void preInstantiateSingletons() throws BeansException {
        //todo；判断是否是单例bean，判断是否为FactoryBean
        beanDefinitionMap.keySet().forEach(this::getBean);
    }

    @Override
    protected BeanFactory getBeanFactory() {
        return this;
    }
}